package com.example.music.utils;

import com.example.music.entity.Consumer;
import com.example.music.entity.Singer;
import com.example.music.entity.Song;
import com.example.music.entity.SongList;
import com.example.music.service.ConsumerService;
import com.example.music.service.SingerService;
import com.example.music.service.SongListService;
import com.example.music.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class FileDel {
    @Autowired
    SongService songService;
    @Autowired
    SingerService singerService;
    @Autowired
    SongListService songListService;
    @Autowired
    ConsumerService consumerService;

    // 每天3点对文件进行删除
    @Async
    @Scheduled(cron = "0 0 3 * * ? ")
    public void delFile() throws FileNotFoundException {

        List<File> list = FileFinder.getInstance(ContentValue.userDir+ContentValue.Slash+"img")
                .registerFileFountListener(file -> System.out.print(""))
                .doFind();
        List<File> list1 = FileFinder.getInstance(ContentValue.userDir+ContentValue.Slash+"avatorImages")
                .registerFileFountListener(file -> System.out.print(""))
                .doFind();
        List<File> list2 = FileFinder.getInstance(ContentValue.userDir + ContentValue.Slash + "song")
                .registerFileFountListener(file -> System.out.print(""))
                .doFind();

        list.addAll(list1);
        list.addAll(list2);
        HashMap<String, File> Map = new HashMap<>();
        for (File file : list) {
            String replace = file.getPath().replace(ContentValue.userDir, "");
            replace = replace.replace("\\", "/");
            Map.put(replace,file);
        }
        ArrayList<String> filePathList = new ArrayList<>();
        List<Singer> singers = singerService.selectAll();
        List<Song> songs = songService.selectAll();
        List<SongList> songLists = songListService.selectAll();
        List<Consumer> consumers = consumerService.selectAll();
        singers.forEach(e->{
            filePathList.add(e.getPic());
        });
        songs.forEach(e->{
            filePathList.add(e.getPic());
            filePathList.add(e.getUrl());
        });
        songLists.forEach(e->{
            filePathList.add(e.getPic());
        });
        consumers.forEach(e->{
            filePathList.add(e.getAvator());
        });

        AtomicInteger index = new AtomicInteger();
        Map.forEach((key,value)->{
            if (!filePathList.contains(key)){
                System.out.println(value.getName()+" : "+ value.getPath());
                value.delete();
                index.getAndIncrement();
            }
        });



    }


}
